<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Cache;
use think\Db;

/**
 * 系统配置
 * Class System
 * @package app\admin\controller
 */
class System extends AdminBase
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 站点配置
     */
    public function siteConfig()
    {
        $site_config = Db::name('system')->field('value')->where('name', 'site_config')->find();
        $site_config = unserialize($site_config['value']);

        return $this->fetch('site_config', ['site_config' => $site_config]);
    }

    /**
     * 更新配置
     */
    public function updateSiteConfig()
    {
        if ($this->request->isPost()) {
            $site_config                = $this->request->post('site_config/a');
            $site_config['site_tongji'] = htmlspecialchars_decode($site_config['site_tongji']);
            $data['value']              = serialize($site_config);
            if (Db::name('system')->where('name', 'site_config')->update($data) !== false) {
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }
    }

    /**
     * 清除缓存
     */
    public function clear()
    {
        if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
            $this->success('清除缓存成功');
        } else {
            $this->error('清除缓存失败');
        }
    }
    /*
     * 轮播图公告
     * */
    public function systemindex(){
        $info = Db::name('system')->field('value')->where('name', 'img_notice')->find();
        $data = unserialize($info['value']);
        return $this->fetch('systemindex',['info'=>$data,'user'=>$GLOBALS['shouquan']]);
    }
    /*
     * 保存公告轮播图
     * */
    public function systemsave(){
        if ($this->request->isPost()) {
            $data= $this->request->post();
            $save['value']= serialize($data);
            if (Db::name('system')->where('name', 'img_notice')->update($save) !== false) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    public function save()
    {
        $data=I('post.');
        $indexinfo=[];
        $indexi='0';
        foreach($data['logo'] as $k=>$v){
            if($v){
                $indexinfo[$indexi]['logo']=$v;
                $indexinfo[$indexi]['pass']=$data['pass'][$k];
                $indexi++;
            }
        }
        $indexsave['value']=serialize($indexinfo);
        $indexid=M('sys_config')->where(array('key'=>'indexlogo'))->getField('id');
        if($indexid)
            $r=M('sys_config')->where(array('id'=>$indexid))->save($indexsave);
        else {
            $indexsave['key']='indexlogo';
            $r = M('sys_config')->add($indexsave);
        }
        if($r!==false){
            $indexmsg='首页轮播图保存成功';
        }else{
            $indexmsg='首页轮播图保存失败';
        }
        $textinfo=[];
        $texti='0';
        foreach($data['textlogo'] as $k=>$v){
            if($v){
                $textinfo[$texti]['textlogo']=$v;
                $textinfo[$texti]['textpass']=$data['textpass'][$k];
                $texti++;
            }
        }
        $textsave['value']=serialize($textinfo);
        $textid=M('sys_config')->where(array('key'=>'textlogo'))->getField('id');
        if($textid)
            $t=M('sys_config')->where(array('id'=>$textid))->save($textsave);
        else {
            $indexsave['key']='textlogo';
            $t = M('sys_config')->add($indexsave);
        }
        if($t!==false){
            $textmsg='热文轮播图保存成功';
        }else{
            $textmsg='热文轮播图保存失败';
        }
        if($r!==false||$t!==false){
            $this->splash(true,$indexmsg.$textmsg,U('SysAdmin/System/index'));
        }else {
            $this->splash(false, $indexmsg.$textmsg);
        }
    }


    /**
     * 账户配置
     */
    public function accountConfig()
    {
        $account_config = Db::name('system')->field('value')->where('name', 'account_config')->find();
        $account_config = unserialize($account_config['value']);
//        halt($account_config);
        return $this->fetch('account_config', ['account_config' => $account_config,'img_url'=>'http://' . $_SERVER['SERVER_NAME'] .'/public']);
    }

    /**
     * 更新账户配置
     */
    public function updateAccountConfig()
    {
        if ($this->request->isPost()) {
            $account_config                = $this->request->post('account_config/a');
//            if(!isset($account_config['pay_type'])){
//                $this->error('至少选一项付款方式');
//            }
//            $account_config['site_tongji'] = htmlspecialchars_decode($account_config['site_tongji']);
            $data['value']              = serialize($account_config);
            if (Db::name('system')->where('name', 'account_config')->update($data) !== false) {
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }
    }






}